package net.jltec.view.base;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import net.jltec.utils.CallBackMethod;
import net.jltec.utils.ProcessMethod;
import net.jltec.view.bean.User;
import net.jltec.view.exception.MyException;

import org.apache.commons.beanutils.ConvertUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.Toast;
@SuppressWarnings({"unchecked","rawtypes"})
public abstract class JltecBaseUI extends Activity {
	
	//配置文件
	protected SharedPreferences configs;
	//等待条
	protected ProgressDialog progressDialog;
	private final String BASE_KEY="PARAMS_KEY";
	private final int OPT_NET_METHOD=1;
	private final int OPT_MAIN_THREAD_METHOD=2;
	
	protected Handler baseHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case OPT_NET_METHOD:
				processMessage(msg);
				break;
			case OPT_MAIN_THREAD_METHOD:
				processMainThreadMethod(msg);
				break;
			default:
				break;
			}
		}
	};
	
	private void processMainThreadMethod(Message msg){
		CallBackMethod listener = (CallBackMethod)msg.obj;
		if(listener!=null){
			listener.execute(null);
		}
	}
	
	
	private void processMessage(Message msg){
		CallBackMethod listener = (CallBackMethod)msg.obj;
		Object result = msg.getData();
		if(listener !=null ){
			Object rs = null;
			if(result!=null){
				Bundle bundle = (Bundle) result;
				rs = bundle.get(BASE_KEY);
			}
			if (rs != null && rs instanceof MyException) {
				MyException ex = (MyException) rs;
				showToast(ex.getMessage());
			} else {
				listener.execute(rs);
			}
		}
		progressDialog.dismiss();
	}
	
	
	protected void asynMethod(String title ,final ProcessMethod processMethod,final CallBackMethod method){
		createProgressDialog(title); 
		new Thread() {
			@Override
			public void run() {
				Object params = null;
				try{
					params = processMethod.execute();
				}catch (Exception e) {
					params = new MyException("网络异常,操作失败!");
				}
				
				Message msg = new Message();
				Bundle data = new Bundle();
				data.putSerializable(BASE_KEY, (Serializable)params);
				msg.setData(data);
				msg.obj = method;
				msg.what=OPT_NET_METHOD;
				baseHandler.sendMessage(msg);
			}
		}.start();
		 
	}
	
	protected void asynMethod(final ProcessMethod processMethod,final CallBackMethod method){
		 asynMethod("正在加载...", processMethod, method);
	}
	
	/**
	 * 在主线程中执行方法
	 * @param method
	 */
	protected void sendMainThreadMethod(final CallBackMethod method){
		Message msg = new Message();
		msg.obj = method;
		msg.what=OPT_MAIN_THREAD_METHOD;
		baseHandler.sendMessage(msg);
	}
	 


	

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
	
	private void init() {
		configs = PreferenceManager.getDefaultSharedPreferences(this);
	}
	
	/**
	 * 创建一个提示信息
	 * @param msg 提示内容
	 */
	protected void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
	
	/**
	 * 生成一个loading等待条
	 * @param title 等待标题
	 * @return
	 */
	protected ProgressDialog createProgressDialog(String title) {
		if(progressDialog==null){
			progressDialog = new ProgressDialog(this);
		}
		progressDialog.setMessage(title);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		progressDialog.show();
		
		return progressDialog;
	}
	
	 
	
	 
	protected <T> T convert(Object o,Class<T> t){
		return (T) ConvertUtils.convert(o, t);
	}
	
	protected Map createOAuthUserMap(){
		HashMap m = new HashMap();
		String oauthflag = configs.getString(User.OAUTH_TOKEN_SECRET, "");
		m.put(User.OAUTH_TOKEN_SECRET, oauthflag);
		return m;
	}
	
	private PopupWindow helpPopupMenu;
	protected void showHelpPopupMenu(View layoutView,View atView){
		helpPopupMenu = new PopupWindow(this);
		helpPopupMenu.setContentView(layoutView);
		helpPopupMenu.setBackgroundDrawable(new BitmapDrawable());
		helpPopupMenu.setFocusable(true);
		helpPopupMenu.setTouchable(true);
		helpPopupMenu.setOutsideTouchable(true);
		helpPopupMenu.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		helpPopupMenu.showAsDropDown(atView);
	 }

}
